package com.limbo.showapp.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.limbo.showapp.R;
import com.limbo.showapp.base.BaseFragment;
import com.limbo.showapp.bean.BannerBean;
import com.limbo.showapp.bean.HotBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.presenter.BannerPresenter;
import com.limbo.showapp.util.SPUtils;
import com.limbo.showapp.view.IHttpView;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by wangqi on 2018/9/4.
 */

public class BannerFragment extends BaseFragment<IHttpView, BannerPresenter> implements IHttpView<BannerBean> {

    private ConvenientBanner<BannerBean.Banner> bannerView;

    @Override
    protected BannerPresenter getPresenter() {
        return new BannerPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_banner;
    }

    @Override
    public void initView(View view) {
        bannerView = view.findViewById(R.id.banner);
    }

    @Override
    public void initData() {
        //设置缓存数据
        BannerBean bannerBean = (BannerBean) SPUtils.getObject(getContext(), AppConstant.SP.DATA_MAIN_BANNER);
        setBannerData(bannerBean);

        mPresenter.getBanner();
    }

    @Override
    public void onSuccess(BannerBean bean) {
        SPUtils.setObject(getContext(), AppConstant.SP.DATA_MAIN_BANNER,bean);

        setBannerData(bean);
    }

    private void setBannerData(BannerBean bean) {
        if (bean==null)
            return;

        bannerView.setPages(new CBViewHolderCreator<ImageViewHolder>() {
            @Override
            public ImageViewHolder createHolder() {
                return new ImageViewHolder();
            }
        }, bean.getData())
                .setPageIndicator(new int[]{R.mipmap.icon_banner_indicator_normal, R.mipmap.icon_banner_indicator_focused})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (bannerView != null)
            bannerView.startTurning(3000);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (bannerView != null)
            bannerView.stopTurning();
    }

    class ImageViewHolder implements Holder<BannerBean.Banner> {

        private ImageView imageView;
        private SparseArray<Bitmap> array = new SparseArray<>();

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, BannerBean.Banner data) {

            final String url = AppConstant.Url.BASE_URL + data.getImgUrl();

            RequestOptions options = new RequestOptions()
//                .placeholder(R.drawable.img_loading)
//                .error(R.drawable.img_error)
                    //设置图片加载的优先级
                    .priority(Priority.HIGH)
                    //缓存策略,跳过内存缓存【此处应该设置为false，否则列表刷新时会闪一下】
                    .skipMemoryCache(false)
                    //缓存策略,硬盘缓存-仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的）
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context).load(url)
                    //动画效果
                    .transition(withCrossFade())
                    .thumbnail(0.1f)
                    .apply(options)
                    .into(imageView);
        }
    }

}
